package com.zhiwang.planet.mvvm.viewmodel.entertain

import android.view.View
import androidx.databinding.ObservableBoolean
import androidx.databinding.ObservableField
import androidx.databinding.ObservableInt
import androidx.databinding.ObservableList
import com.zhiwang.planet.R
import com.zhiwang.planet.mvvm.viewmodel.ShareViewModel
import com.zhiwang.planet.mvvm.viewmodel.main.sharedViewModels

class PlayedGameItemViewModel constructor(var checked: ObservableBoolean,
                                          var gameId: ObservableField<String>,
                                          var gameName: ObservableField<String>,
                                          var icon: ObservableField<String>,
                                          var tags: ObservableList<String>,
                                          var profit: ObservableInt) {

    var checkVisible = ObservableBoolean(false)

    var checkbg  = ObservableInt(R.color.game_unchecked_color)

    init {


        PlayGameConnection.selectActive.observeForever {
            checkVisible.set(it)
        }

        PlayGameConnection.cancelGame.observeForever { it: List<String> ->
            if (it.contains(gameId.get()))
                checked.set(false)
        }
    }


    val shareViewModel by sharedViewModels<ShareViewModel>()

    fun itemClick() {
        if (PlayGameConnection.selectActive.value == true)
            checked.set(checked.get().not())

        if (checked.get()) {
            PlayGameConnection.checkedGame.value = gameId.get()
        } else PlayGameConnection.uncheckedGame.value = gameId.get()
    }


    fun activeSelect(v: View): Boolean {

        PlayGameConnection.selectActive.value = true
        return true

    }

    fun play() {
        if (PlayGameConnection.selectActive.value == true)
            return
        PlayGameConnection.playGame.value = gameId.get()
    }
}